package cn.enjoy.boot.controller;

import cn.enjoy.boot.entity.Fake;
import cn.enjoy.boot.mapper.FakeMapper;
import cn.enjoy.redisson.util.RedisUtil;
import cn.enjoy.tool.api.R;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author K
 * @date 2021-11-10 21:45
 */
@RequestMapping("fake")
@RestController
@AllArgsConstructor
public class FakeController {

    private final FakeMapper fakeMapper;
    private RedisUtil redisUtil;

    @GetMapping("/setCache")
    public Fake setCache(Long id, String key) {
        Fake fake = fakeMapper.getById(id);
        redisUtil.set(key, fake);
        return fake;
    }

    @GetMapping("/getCache")
    public R getCache(String key) {
        Object value = redisUtil.get(key, cn.enjoy.boot.entity.redis.Fake.class);

        return R.data(value);
    }

    @GetMapping("/page")
    public List<Fake> fakePage(Long id) {
        Page<Fake> page = new Page<>();
        page.setCurrent(1);
        page.setSize(200);
        Page<Fake> fakePage = fakeMapper.selectPage(page, Wrappers.emptyWrapper());

        return fakePage.getRecords();
    }

    @GetMapping("/get")
    public Fake get(Long id) {
        return fakeMapper.getById(id);
    }

    @PostMapping("/insert")
    public R insert(@RequestBody Fake fake) {
        int flag = fakeMapper.insert(fake);

        return R.status(flag == 1);
    }

    @PostMapping("/insert/form")
    public R insert(@RequestParam String name, @RequestParam Long amount) {
        Fake fake = new Fake();
        fake.setName(name);
        fake.setAmount(amount);
        int flag = fakeMapper.insert(fake);

        return R.status(flag == 1);
    }

}
